package com.genesis.yunnanji.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.SupAdapter;
import com.genesis.yunnanji.bean.SupBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;



public class Sup extends Fragment {
    private RecyclerView mRecyclerView;
    private int page = 1;
    private List<SupBean.ResultBean.InfoBean> list;
    private SupAdapter adapter;
    private TwinklingRefreshLayout refreshLayout;
    private Toolbar mToolbar;
    private View view;
    public View nodata;

    public static Sup newInstance(String param) {
        Sup obj = new Sup();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.activity_sup, null);
//            StatusBarUtil.setColor(getActivity(), getResources().getColor(R.color.main_color), 0);
            nodata = view.findViewById(R.id.layout_nodata);
            initView(view);
            setUpView();
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        return view;
    }

    protected void initView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_sup_list);
        refreshLayout = (TwinklingRefreshLayout) view.findViewById(R.id.bga_sup_refresh);
        mToolbar = (Toolbar) view.findViewById(R.id.tb_sup_toolbar);
        mToolbar.setVisibility(View.VISIBLE);
        list = new ArrayList<>();
        mToolbar.setTitle("");

        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).size((int) getResources().getDimension(R.dimen.x5)).color(Color.parseColor("#f5f5f5")).build());
        getData(null);
    }


    protected void setUpView() {
        LoadingView loadingView = new LoadingView(getActivity());
        refreshLayout.setBottomView(loadingView);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                page = 1;
                list.removeAll(list);
                getData(refreshLayout);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                page += 1;
                getData(refreshLayout);
            }
        });
    }

    private void getData(final TwinklingRefreshLayout refreshLayout) {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.SUP_LIST);
        params.addBodyParameter("p", page + "");
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                try {
                    JSONArray jsonArray = jsonObject.getJSONObject("result").getJSONArray("list");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        SupBean.ResultBean.InfoBean infoBean = gson.fromJson(jsonArray.get(i).toString(), SupBean.ResultBean.InfoBean.class);
                        list.add(infoBean);
                    }
                    if (list.size() > 0) {
                        nodata.setVisibility(View.GONE);
                    } else {
                        nodata.setVisibility(View.VISIBLE);
                    }
                    initList(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
                if (refreshLayout != null) {
                    refreshLayout.finishRefreshing();
                    refreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

    private void initList(final List<SupBean.ResultBean.InfoBean> list) {
        if (page == 1) {
            adapter = new SupAdapter(getContext(), list);

            mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            mRecyclerView.setNestedScrollingEnabled(false);
            mRecyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                    Log.e("id", list.get(position).getId());

                    Intent intent = new Intent(getContext(), SupDetails.class);
                    intent.putExtra("supid", list.get(position).getId());
                    startActivity(intent);
                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }

}
